#include<vector>
#include<unordered_map>
using std::unordered_map;
using std::vector;


class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        int n = nums.size();
        int pre = 0;
        int count = 0;
        unordered_map<int, int> mp = {{0, 1}};
        for (int i = 0; i < n; i++) {
            pre = pre + nums[i];
            int key = pre - k;
            if (mp.find(key) != mp.end()) {
                count += mp[key];
            }
            if (mp.find(pre) != mp.end()) mp[pre]++;
            else mp[pre] = 1;
        }
        return count;
    }

    
};